<?php
class DB
{
    public $config = ['host' => 'localhost', 'user' => 'root', 'pwd' => 'root', 'db' => 'br_shop'];
    public $link;

    function __construct()
    {
        $this->link = $this->connect();
    }

    // 创建链接
    function connect()
    {
        $link = mysqli_connect($this->config['host'], $this->config['user'], $this->config['pwd'], $this->config['db']);
        if (!$link) {
            die('数据库链接失败' . mysqli_connect_error());
        }
        return $link;
    }

    function query($sql)
    {
        $resource=mysqli_query($this->link,$sql);
        if(is_bool($resource)){
            $res=$resource;
        }else{
            while($row=mysqli_fetch_assoc($resource)){
                $list[]=$row;
            }
            // 释放内存资源
            mysqli_free_result($resource);
           
            $res=$list;
        }
        // 关闭数据库链接
        $this->close();
        return $res;
    }

    // 只查询一条数据
    function get_one($sql)
    {
        $resource=mysqli_query($this->link,$sql);
        if(is_bool($resource)){
            $res=$resource;
        }else{
            // 支取一条数据
            $res=mysqli_fetch_assoc($resource);
            // 释放内存资源
            mysqli_free_result($resource);
        }
        // 关闭数据库链接
        $this->close();
        return $res;
    }

    // 关闭链接
    function close()
    {
        mysqli_close($this->link);
    }
}
